package com.lishe.lishe.lishe.module.me.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.common.Globals;
import com.lishe.lishe.lishe.module.auth.view.SendCodeTextView;
import com.lishe.lishe.lishe.module.me.adapter.AddressAdapter;
import com.lishe.lishe.lishe.module.me.bean.AddressItem;
import com.lishe.lishe.lishe.module.me.http.HttpUserCenter;
import com.lishe.lishe.lishe.utils.LoadingUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by zhcasecom on 2017/8/18.
 */

public class AddressManageActivity extends BaseActivity implements AddressAdapter.Callback, View.OnClickListener {
    private String TAG = getClass().getSimpleName();
    private Button add_address;
    private ListView addr_list;
    private List<AddressItem> address_list;

    private AddressAdapter addressAdapter;


    private LinkedList<AddressItem> linkedList_address;
    private AddressAdapter linkedList_adapter;

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, AddressManageActivity.class);
        activity.startActivity(intent);
    }

    public static void jumpForResult(Activity activity, int requseCode) {
        Intent intent = new Intent(activity, AddressManageActivity.class);
        activity.startActivityForResult(intent, requseCode);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_manage);
        initView();
    }

    private void initView() {
        add_address = findViewById(R.id.add_address);
        add_address.setOnClickListener(this);
        addr_list = findViewById(R.id.address_list);

        TextView title = findViewById(R.id.title_text_title);
        title.setText(R.string.title_adddress_manage);
        addr_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        linkedList_address = new LinkedList<>();
        linkedList_adapter = new AddressAdapter(AddressManageActivity.this, linkedList_address, AddressManageActivity.this);
        addr_list.setAdapter(linkedList_adapter);
        Button back = findViewById(R.id.title_left_button);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setResult(RESULT_OK, getIntent());
                finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        HttpUserCenter.getAddressList(null, response);
        linkedList_adapter.notifyDataSetChanged();
    }

    @Override
    public void click(View v) {
        final int i = (int) v.getTag();
        switch (v.getId()) {
            case R.id.edit_addr:
                //Log.e(TAG, "点击了第" + i + "个修改按钮");
                String addr_id = address_list.get(i).getAddr_id();
                AddAddrActivity.jump(AddressManageActivity.this, Globals.UPDATE_ADRESS, addr_id);
                break;

            case R.id.set_remote_address:
                //Log.e(TAG, "点击了第" + i + "个设置默认地址按钮");
                LoadingUtils.createLoadingDialog(AddressManageActivity.this, "请稍后。。。",true);
                changeDefaultAddr(i);
                break;
        }

    }

    private void changeDefaultAddr(int ii) {
        HashMap params = new HashMap();
        String addr_id = address_list.get(ii).getAddr_id();
        Log.e(TAG, "addr_id_____" + addr_id);
        params.put("addrId", addr_id);
        HttpUserCenter.changeDefaultAddr(params, response_change_defaultaddr);

    }


    /**
     * 获取地址列表
     */
    LSWebHelper.WebCallBack response = new LSWebHelper.WebCallBack() {
        @Override
        public void onSuccess(JSONObject dataJson, String dataString, String message) {
            LoadingUtils.closeDialog();
            String data = dataString;
            Log.e(TAG, "Address_list_data" + data);

            address_list = JSON.parseArray(dataString, AddressItem.class);
            if (address_list != null && address_list.size() >= 0) {
                linkedList_address.clear();
                linkedList_address.addAll(address_list);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        linkedList_adapter.resetData(linkedList_address);
                    }
                });
            }
        }


        @Override
        public void onFailure(String message) {
            Toast.makeText(AddressManageActivity.this, message, Toast.LENGTH_SHORT).show();
            LoadingUtils.closeDialog();
        }
    };
    /**
     * 修改默认地址回调
     */
    LSWebHelper.WebCallBack response_change_defaultaddr = new LSWebHelper.WebCallBack() {
        @Override
        public void onSuccess(JSONObject dataJson, String dataString, String message) {
            JSONObject data = dataJson;
            Log.e(TAG, "changeAddr_data" + data);

            HttpUserCenter.getAddressList(null, response);
        }

        @Override
        public void onFailure(String message) {
            Toast.makeText(AddressManageActivity.this, message, Toast.LENGTH_SHORT).show();
            LoadingUtils.closeDialog();
        }
    };

    @Override
    public void onClick(View view) {
        AddAddrActivity.jump(AddressManageActivity.this, Globals.ADD_ADRESS, "");
    }

    /**
     * 发送广播通知上一页面更新UI
     */
    private void sendUpUIBroadcast() {
        Intent intent = new Intent();
        intent.setAction(Globals.ACTION_UPADDRESS);
        sendBroadcast(intent);
    }

    @Override
    public void finish() {
        super.finish();
        sendUpUIBroadcast();
    }
}
